<?php

$page_security = 'SA_SALESTRANSVIEW';
$path_to_root = "..";

include_once ($path_to_root . "/sales/includes/cart_class.inc");
include_once ($path_to_root . "/manufacturing/includes/manufacturing_ui.inc");
include_once ($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/includes/date_functions.inc");

$js = '';

if ($use_popup_windows)
{
    $js .= get_js_open_window(900, 500);
}

//if ($use_date_picker) {
//	$js .= get_js_date_picker();
//}

get_js_datetime_picker();

$Potong_Items = array();
$Jahit_Items = array();
$Sablon_Items = array();
$Finish_Items = array();

$mode = BAG_POTONG;

page(_($help_context = "Kartu Produksi"));


simple_page_mode(true);
processing_start();

start_form();

function can_process()
{

    return true;
}

if (isset($_POST['SaveKartu']))
{
    close_production_card(get_post('wo_item'));
    meta_forward($path_to_root, "application=manuf");
}

if (isset($_GET['selected_id']))
{
    $selected_id = $_GET['selected_id'];
}

if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM')
{
    if ($selected_id != -1)
    {
        update_production_card_item(get_post('date_s'), get_post('selected_id'));
        display_notification(_('Item yang dipilih berhasil diperbaharui.'));
    } else
    {

        add_production_card(get_post('bag_pro'), get_post('date_t'), get_post('component'),
            get_post('quantity'), get_post('karyawan_id'), get_post('wo_item'));
        display_notification(_('Penerima Bahan Baku Berhasil ditambahkan.'));
    }
    $Mode = 'RESET';
}

if ($Mode == 'Delete')
{

    del_production_card($selected_id);
    display_notification(_('Selected item has been deleted'));
    $Mode = 'RESET';
}

if ($Mode == 'RESET')
{
    $selected_id = -1;
    $wo = get_post('wo_item');
    unset($_POST);
    $_POST['wo_item'] = $wo;
    // 	meta_forward($_SERVER['PHP_SELF'], "wo_item=$wo");
}

if (!isset($_GET['wo_item']) && !isset($_POST['wo_item']))
{
    display_error('Halaman ini tidak bisa diakses langsung');
    exit;
}

if (isset($_GET['wo_item']) && is_string($_GET['wo_item']))
{
    $_SESSION['Manufacturing_Edit'] = $_GET['wo_item'];
    $_SESSION['Potong_Items'] = get_production_card_array(BAG_POTONG, $_GET['wo_item']);
    $_SESSION['Jahit_Items'] = get_production_card_array(BAG_JAHIT, $_GET['wo_item']);
    $_SESSION['Sablon_Items'] = get_production_card_array(BAG_SABLON, $_GET['wo_item']);
    $_SESSION['Finish_Items'] = get_production_card_array(BAG_FINISH, $_GET['wo_item']);
    hidden('wo_item', $_GET['wo_item']);
}
if (isset($_POST['wo_item']) && is_string($_POST['wo_item']))
{
    $_SESSION['Manufacturing_Edit'] = $_POST['wo_item'];
    $_SESSION['Potong_Items'] = get_production_card_array(BAG_POTONG, $_POST['wo_item']);
    $_SESSION['Jahit_Items'] = get_production_card_array(BAG_JAHIT, $_POST['wo_item']);
    $_SESSION['Sablon_Items'] = get_production_card_array(BAG_SABLON, $_POST['wo_item']);
    $_SESSION['Finish_Items'] = get_production_card_array(BAG_FINISH, $_POST['wo_item']);
    hidden('wo_item', $_POST['wo_item']);
}

if (isset($_SESSION['Potong_Items']))
    $Potong_Items = $_SESSION['Potong_Items'];

if (isset($_SESSION['Jahit_Items']))
    $Jahit_Items = $_SESSION['Jahit_Items'];

if (isset($_SESSION['Sablon_Items']))
    $Sablon_Items = $_SESSION['Sablon_Items'];

if (isset($_SESSION['Finish_Items']))
    $Finish_Items = $_SESSION['Finish_Items'];


$wo_id = $_SESSION['Manufacturing_Edit'];

if (isset($_POST["AddItemPotong"]))
{
    add_production_card(BAG_POTONG, get_post('date1'), get_post('bhn'), get_post('jml'),
        get_post('emp_id'), $wo_id);
    unset($_POST['bhn']);
}

br();
//start_table(TABLESTYLE, "width=95%", 5);
start_outer_table(TABLESTYLE2, "width=95%", 5);

table_section(1);
$result = get_sales_order_details_view($wo_id);
$woitems = db_fetch($result);
label_row("Kode Produk:", $woitems['stk_code']);
label_row('Diskripsi:', $woitems['description']);
label_row('Reference:', '');
table_section(2);
label_row("Tanggal Pesan :", sql2date($woitems['ord_date']));
label_row("Tanggal Dibutuhkan :", sql2date($woitems['delivery_date']));
$jmlpsn = get_sales_order_item_details_sum_pesanan($wo_id);
label_row("Jumlah Pesanan:", $jmlpsn['jumlah_pesanan']);
end_outer_table();
//tabel bagian potong
div_start('tables_');
start_table(TABLESTYLE, "width=95%");
//display_heading2(_("Bagian Potong"));
echo "<br/><span style='float:left;margin-left:30px;font-weight:bold'>Bagian Potong</span>";
$th = array(
    _("Penerima"),
    _("ID Bahan Baku"),
    _("Nama Bahan Baku"),
    _("Qty"),
    _("Waktu Terima"),
    _("Waktu Selesai"),
    '',
    '');
inactive_control_column($th);
table_header($th);
$result = get_production_card(BAG_POTONG, $wo_id);
$k = 0;

//db_pager();
//$Potong_Items = db_fetch_row($result);
//var_dump($Potong_Items);
//var_dump();

foreach ($Potong_Items as $myrow)
{

    alt_table_row_color($k);

    label_cell($myrow["empname"]);
    label_cell($myrow["item_id"]);
    label_cell($myrow["description"]);
    label_cell($myrow["item_amount"]);
    label_cell(sql2datetime($myrow["waktu_diterima"]));
    label_cell(sql2datetime($myrow["waktu_selesai"]));

    edit_button_cell("Edit" . $myrow["cardid"], _("Edit"));
    delete_button_cell("Delete" . $myrow["cardid"], _("Delete"));
    end_row();

}
end_table();

start_table(TABLESTYLE, "width=95%");
echo "<br/><span style='float:left;margin-left:30px;font-weight:bold'>Bagian Sablon</span>";
$th = array(
    _("Penerima"),
    _("ID Bahan Baku"),
    _("Nama Bahan Baku"),
    _("Qty"),
    _("Waktu Terima"),
    _("Waktu Selesai"),
    '',
    '');
inactive_control_column($th);
table_header($th);
$result1 = get_production_card(BAG_SABLON, $wo_id);
$k = 0;

foreach ($Sablon_Items as $myrow)
{

    alt_table_row_color($k);

    label_cell($myrow["empname"]);
    label_cell($myrow["item_id"]);
    label_cell($myrow["description"]);
    label_cell($myrow["item_amount"]);
    label_cell(sql2datetime($myrow["waktu_diterima"]));
    label_cell(sql2datetime($myrow["waktu_selesai"]));
    edit_button_cell("Edit" . $myrow["cardid"], _("Edit"));
    delete_button_cell("Delete" . $myrow["cardid"], _("Delete"));
    end_row();

}

end_table();

start_table(TABLESTYLE, "width=95%");
echo "<br/><span style='float:left;margin-left:30px;font-weight:bold'>Bagian Jahit</span>";
$th = array(
    _("Penerima"),
    _("ID Bahan Baku"),
    _("Nama Bahan Baku"),
    _("Qty"),
    _("Waktu Terima"),
    _("Waktu Selesai"),
    '',
    '');
inactive_control_column($th);
table_header($th);
$result2 = get_production_card(BAG_JAHIT, $wo_id);
$k = 0;

foreach ($Jahit_Items as $myrow)
{

    alt_table_row_color($k);

    label_cell($myrow["empname"]);
    label_cell($myrow["item_id"]);
    label_cell($myrow["description"]);
    label_cell($myrow["item_amount"]);
    label_cell(sql2datetime($myrow["waktu_diterima"]));
    label_cell(sql2datetime($myrow["waktu_selesai"]));
    edit_button_cell("Edit" . $myrow["cardid"], _("Edit"));
    delete_button_cell("Delete" . $myrow["cardid"], _("Delete"));
    end_row();

}

end_table();

start_table(TABLESTYLE, "width=95%");
echo "<br/><span style='float:left;margin-left:30px;font-weight:bold'>Bagian Finishing</span>";
$th = array(
    _("Penerima"),
    _("ID Bahan Baku"),
    _("Nama Bahan Baku"),
    _("Qty"),
    _("Waktu Terima"),
    _("Waktu Selesai"),
    '',
    '');
inactive_control_column($th);
table_header($th);
$result3 = get_production_card(BAG_FINISH, $wo_id);
$k = 0;
foreach ($Finish_Items as $myrow)
{

    alt_table_row_color($k);

    label_cell($myrow["empname"]);
    label_cell($myrow["item_id"]);
    label_cell($myrow["description"]);
    label_cell($myrow["item_amount"]);
    label_cell(sql2datetime($myrow["waktu_diterima"]));
    label_cell(sql2datetime($myrow["waktu_selesai"]));
    edit_button_cell("Edit" . $myrow["cardid"], _("Edit"));
    delete_button_cell("Delete" . $myrow["cardid"], _("Delete"));
    end_row();

}
end_table();
div_end();
br();

div_start('details');
start_table(TABLESTYLE2);


//if (list_updated('bag_pro'))
//{
//    if(get_post('bag_pro') == BAG_POTONG)
//    $_POST['satuan'] = 'KG';
//
//    $Ajax->activate('sat');
//}

if ($selected_id != -1)
{

    //editing an existing item category
    $myrow = get_production_card_item($selected_id);
    label_row("Bagian Produksi :", $bagian_produksi[$myrow["typeid"]]);
    label_row("Nama Karyawan :", $myrow["empname"]);
    label_row("Bahan Baku :", $myrow["item_id"] . " - " . $myrow["description"]);
    label_row("Jumlah :", $myrow["item_amount"]); // TODO: satuan
    label_row("Waktu Terima:", sql2date($myrow["waktu_diterima"]));

    hidden('selected_id', $selected_id);

} else
{
    bagian_produksi_types_list_row('Bagian Produksi :', 'bag_pro');

    employee_list_row('Nama Karyawan :', 'karyawan_id', null, false, true);
    start_row();
    label_cell("Bahan Baku", "class='label'");
    echo "<td>";
    echo stock_component_items_list('component', 'bahan', null, false, true);

    $_POST['enablesat'] = true;
    $_POST['initsat'] = '';

    if (get_post('bag_pro') == BAG_POTONG)
    {
        $_POST['enablesat'] = false;
        //var_dump(get_post('component'));
//        var_dump(get_stock_unit(get_post('component')));
//        var_dump(get_item_unit_abbr_by_name(get_stock_unit(get_post('component'))));
        $_POST['initsat'] = get_stock_unit(get_post('component'));        
    } else
    {
        $_POST['enablesat'] = true;
    }
    $Ajax->activate('sat');

    echo "</td>";
    end_row();
    $dec = get_qty_dec(get_post('component'));
    $_POST['quantity'] = number_format2(input_num('quantity', 1), $dec);

    qty_row("Jumlah:", 'quantity', null, null, null, $dec);
    stock_units_list_row('Satuan:', 'sat', $_POST['initsat'], $_POST['enablesat']);

    datetime_row('Waktu Terima:', 'date_t', '', 'example1');
}
datetime_row('Waktu Selesai:', 'date_s', '', 'example2');
end_table(1);
div_end();
echo "<table align=center><tr><td>";
if ($selected_id == -1)
{
    //submit('ADD_ITEM', _("Add new"), true, $title, $async);

    submit_js_confirm('SaveKartu', "Anda yakin akan melakukan \nTUTUP KARTU PRODUKSI?");
    submit('SaveKartu', "Tutup Kartu Produksi", true, '', true);
}
submit_add_or_update($selected_id == -1, '', true);
echo "</td></tr></table>";
br();

end_form();
end_page();
// $Ajax->activate('details');
// $Ajax->activate('tables_');


?>